/**
 * Created by zqk on 2018/4/16.
 */
$(document).ready(function () {
    var iProjectGrade=addressUri.getParam('iProjectGrade');
    var iParentPid=addressUri.getParam('iParentPid');
    var iProjectId=addressUri.getParam('iProjectId');
    var memberList= $('#memberList');
    memberList.datagrid({
        url:moduleUrl+'project/getProjectMemberList?iProjectGrade='+iProjectGrade+'&iParentPid='+iParentPid,
        method: 'post',
        rownumbers: true,
        pagination: true,
        pageSize: 20,
        pageList: [20,50,100,200],
        idField: 'iUserId',
        singleSelect:false,
        columns:[[
            {field:'ck',checkbox:true},
            {field:'iUserId',title:'iUserId',hidden:true},
            {field:'sStaffName',title:'员工姓名',width:180},
            {field:'sPostName',title:'员工岗位',width:180},
        ]],
        onLoadSuccess:function () {
            //获取已添加 或者默认添加merberIds
            $.ajax({
                url:moduleUrl+'project/getCheckedMemberIds',
                data:{
                    iParentPid:iParentPid,
                    iProjectGrade:iProjectGrade,
                    iProjectId:iProjectId,
                },
                type:'POST',
                dataType:'json',
                success:function (data) {
                    console.log(data);
                    for(x in data[0]){
                        $('#memberList').datagrid('selectRecord',data[0][x]);
                    }

                },
                error:function (data) {
                    console.log(data);
                }
            });

        }

    });



    $('#bt_modal_confirm').click(function () {


        myWindow.initPopUp({title: '编辑项目成员', text: '确认修改该项目成员吗?'},function () {
            var selections = memberList.datagrid('getSelections');
            var memberIds = [];
            for(k in selections ){
                memberIds.push(selections[k].iUserId)
            }
            var data = {
                'iProjectId':iProjectId ,
                'memberIds':memberIds
            }
            $.ajax({
                url:moduleUrl+'project/editProjectMember',
                dataType: "JSON",
                data:data,
                type:'POST',
                success:function (data) {
                    window.top.xhTop.showSuccess('操作成功');
                    window.top.xhTop.closeModal();

                },
                error:function (data) {
                    window.top.xhTop.showError(
                        data.responseJSON.error,
                        data.responseJSON.msg
                    );
                }
            });
        })





    });

    $('#bt_modal_close').click(function () {
        window.top.xhTop.closeModal();
    })

});